package com.dongman.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongman.model.Novel;
import com.dongman.utils.R;
import com.dongman.service.ManhuaSpiderService;
import com.dongman.utils.SteamSpider;
import org.apache.commons.collections4.MapUtils;
import org.apache.http.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.File;

@Controller
@CrossOrigin
public class TestController {

    @RequestMapping("/upload")
    public String upload() {

        String url = "https://test.aqwatches.com/aa/upload.php"; //上传的地址
        String filePath = "E:\\test\\test.txt";

        RestTemplate rest = new RestTemplate();
        FileSystemResource resource = new FileSystemResource(new File(filePath));
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("files", resource); //MultipartFile的名称
        String rs = rest.postForObject(url, param, String.class);
        System.out.println(rs);
        return rs;
    }

    @RequestMapping("/getItemNameIdByLink")
    @ResponseBody
    public String getItemNameIdByLink() {
        SteamSpider spider = new SteamSpider();
        String baseUrl =  "https://steamcommunity.com/market/listings/730/Recoil%20Case";
        return spider.getItemNameIdByLink(baseUrl);
    }


    @Autowired
    ManhuaSpiderService manhuaSpiderService;

    @ResponseBody
    @GetMapping("/one")
    public R one() {
        new Thread(() -> manhuaSpiderService.crawlonelManhuLinks("https://www.qiteman.com/comic/1087.html")).start();
        return R.ok();
    }
    @ResponseBody
    @GetMapping("/all")
    public R all() {
        new Thread(() -> manhuaSpiderService.crawlAllManhuLinks()).start();
        return R.ok();
    }
    @ResponseBody
    @GetMapping("/pic")
    public R pic() {
        Novel novel = new Novel();
        novel.setId("1");
        new Thread(() -> manhuaSpiderService.selectNovelChapterAdnPics(novel)).start();
        return R.ok();
    }

    @ResponseBody
    @GetMapping("/downPic")
    public R downPic(String novelId) {
        new Thread(() -> manhuaSpiderService.crawlAllPicsByChapterLink(novelId)).start();
        return R.ok();
    }

    /**
     * 爬取所有的图片
     * @return
     */
    @ResponseBody
    @GetMapping("/downAllPics")
    public R downAllPics() {
        new Thread(() -> manhuaSpiderService.crawAllPicsToLocal()).start();
        return R.ok();
    }


}
